package session.stateless;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.ejb.Stateless;
import javax.faces.context.FacesContext;

/**
 *
 * @author Swee Zhi
 */
@Stateless
public class SystemMessagesBean {
    private FacesContext ctx = FacesContext.getCurrentInstance();

    public SystemMessagesBean() {
        
    }
    
    public String getSystemMessage(String messageKey, String locale)
    {
//        String locale="";
        String message="";

        
//         future expansion
//        ResourceBundle exceptionResources = ResourceBundle.getBundle("MyResources", currentLocale);
//         fix a locale first
//        ResourceBundle exceptionResources = ResourceBundle.getBundle("ExceptionMessages", Locale.getDefault());
        
//        // properties file has this key
//        if (exceptionResources.containsKey(messageName)) {
//            message = exceptionResources.getString(messageName);
//        }
//        // get default value
//        else {
//           exceptionResources.getString("no.message");
//        }
        
        //create an instance of properties class
        Properties props = new Properties();

        //try retrieve data from file
        //catch exception in case properties file does not exist
        try {
            /* test where your glassfish writes file to.. it will read from there as root 
             * to use testing part, please comment off other parts in the try-catch
             */
            
//            File file = new File("findme.properties");
//
//             Create file if it does not exist
//            boolean success = file.createNewFile();
//            if (success) {
//                System.out.println("Successfully created 'findme.properties'");
//                // File did not exist and was created
//                System.out.println(file.getAbsoluteFile());
//                System.out.println(file.getAbsolutePath());
//                System.out.println(file.getCanonicalFile());
//                System.out.println(file.getCanonicalPath());
//            } else {
//                // File already exists
//                System.out.println("'findme.properties' already exists");
//            }
            /* end test where your glassfish writes file to.. it will read from there as root */
            
            if (locale.equals("en_US")) {
                props.load(new FileInputStream("SystemMessages.properties"));
            }
            else {
                props.load(new FileInputStream("SystemMessages_" + locale + ".properties"));
            }
            message = props.getProperty(messageKey);
            
            //If value of message variable is null assign value
            if (message == null)
            {
                message = props.getProperty("NO_MESSAGE_DEFINED");
            }
        }
        //catch exception in case properties file does not exist
        catch(IOException e)
        {
            e.printStackTrace();
        }
        
        return message;
    }
    
}
